﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WXYW.BLL
{
    public static class RunningNumberService
    {
        public static string GenerateDateRunningNumber()
        {
            return DateTime.Now.ToString("yyMMdd");
        }

        public static string GenerateApplicationNumber()
        {
            string dataNumber = GenerateDateRunningNumber();
            string monthNumber = DateTime.Now.ToString("yyMM");
            ApplicationService service = new ApplicationService();
            int numberOfApp = service.GetNumberOfApplication(monthNumber);
            string numberCode = TransferToRunningNumber(numberOfApp + 1);

            return dataNumber + numberCode;
        }

        public static string GenerateRMANumber(string stationCode)
        {
            //string rmaCode = "R" + stationCode + GenerateDateRunningNumber();
            string dateNumber = GenerateDateRunningNumber();
            string rmaCode = "R" + stationCode + dateNumber;
            ApplicationService service = new ApplicationService();
            int numberOfRMA = service.GetNumberOfRMA(dateNumber);
            string numberCode = TransferToRunningNumber(numberOfRMA + 1);

            return rmaCode + numberCode;
        }

        public static string GenerateDOMNumber(string stationCode)
        {
            string dateNumber = GenerateDateRunningNumber();
            string rmaCode = "D" + stationCode + dateNumber;
            ApplicationService service = new ApplicationService();
            int numberOfRMA = service.GetNumberOfRMA(dateNumber);
            string numberCode = TransferToRunningNumber(numberOfRMA + 1);

            return rmaCode + numberCode;
        }

        public static string GenerateSRNumber(string stationCode)
        {
            string dateNumber = GenerateDateRunningNumber();
            string srCode = "SR" + stationCode + dateNumber;
            ApplicationService service = new ApplicationService();
            int numberOfRMA = service.GetNumberOfRMA(dateNumber);
            string numberCode = TransferToRunningNumber(numberOfRMA + 1);

            return srCode + numberCode;
        }

        //public static string GenerateBJXXNumber()
        //{
        //    string rmaCode = DateTime.Now.ToString("yyyyMMddhhmmss");
        //    ApplicationService service = new ApplicationService();

        //}

        public static string GenerateUpgrateNumber()
        {
            string upCode = GenerateDateRunningNumber();
            UpgradeService service = new UpgradeService();
            int numberOfUpgrade = service.GetNumberOfUpgrade(upCode);
            string numberCode = TransferToRunningNumber(numberOfUpgrade + 1);

            return upCode + numberCode;
        }

        public static string GenerateBJRunningNumber(int serviceStationID)
        {
            string BJCode = GenerateDateRunningNumber();
            ServiceStationService ssservice = new ServiceStationService();
            string serviceStationAbbreviation = ssservice.GetServiceStation(serviceStationID).ForShort;
            SparePartApplyService spaService = new SparePartApplyService();
            int numberOfSparePartApply = spaService.GetNumberOfSparePartApplyForms(BJCode);
            string numberCode = TransferToRunningNumber(numberOfSparePartApply + 1);

            return "BJ" + serviceStationAbbreviation + BJCode + numberCode;
        }

        public static string GenerateReturnToFactoryRunningNumber(string providerCode)
        {
            string rtfCode = GenerateDateRunningNumber();
            ReturnToFactoryService service = new ReturnToFactoryService();
            int numberOfReturnForm = service.GetNumberOfReturnToFactoryForms(rtfCode);
            string numberCode = TransferToRunningNumber(numberOfReturnForm + 1);

            return "DCNRMA" + providerCode + rtfCode + numberCode;
        }

        public static string TransferToRunningNumber(int numberOfApp)
        {
            string result = string.Empty;
            if (numberOfApp > 999)
            {
                result = numberOfApp.ToString();
            }
            else if (numberOfApp > 99)
            {
                result = "0" + numberOfApp.ToString();
            }
            else if (numberOfApp > 9)
            {
                result = "00" + numberOfApp.ToString();
            }
            else
            {
                result = "000" + numberOfApp.ToString();
            }

            return result;
        }

        public static string GenerateBJXXCode()
        {
            string code = DateTime.Now.ToString("yyMMdd");
            string monthCode = DateTime.Now.ToString("yyMM");
            ApplicationProductService productservice = new ApplicationProductService();
            int count = productservice.GetNumberOfBJXXProduct(monthCode);

            return "SPA" + code + TransferToRunningNumber(count);
        }

        public static string GenerateDOACode()
        {
            string code = DateTime.Now.ToString("yyMMdd");
            string monthCode = DateTime.Now.ToString("yyMM");
            ApplicationProductService productservice = new ApplicationProductService();
            int count = productservice.GetNumberOfBJXXProduct(monthCode);

            return "SPD" + code + TransferToRunningNumber(count);
        }

        public static string GenerateRequisitionCode()
        {
            string code = DateTime.Now.ToString("yyMMdd");
            RequisitionService service = new RequisitionService();
            int count = service.GetCountOfForm(code);
            return code + TransferToRunningNumber(count + 1);
        }

        public static string GenerateSpecialServiceCode()
        {
            string code = DateTime.Now.ToString("yyyyMMdd");
            SpecialServiceService service = new SpecialServiceService();
            int count = service.GetCountOfApplication(code);
            return code + TransferToRunningNumber(count + 1);
        }

        public static string GenerateZCApplicationCode()
        {
            string code = DateTime.Now.ToString("yyyyMMdd");
            ZCService service = new ZCService();
            int count = service.GetCountOfApplication(code);
            return code + TransferToRunningNumber(count + 1);
        }
    }
}
